package cn.com.utt.unms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.unms.mapper.LogMapper;
import cn.com.utt.unms.pojo.Log;
import cn.com.utt.unms.service.LogService;
import cn.com.utt.unms.vo.LogSee;

@Service("LogService")
public class LogServiceImpl implements LogService {
	@Autowired
	private LogMapper logMapper;

	@Override
	public void save(Log log) {
		logMapper.save(log);
	}

	public  List<Long> queryChildAll(Long userId) {
		List<Long> result = new ArrayList<>();
		List<Long> list = logMapper.queryLogChildAll(userId);
		if (list == null || list.isEmpty()) {
			return null;
		} 
		else {
			for (Long id : list) {
				List<Long> list1 = null;
				list1 = queryChildAll(id);
				if (list1 != null && !list.isEmpty()) {
					result.addAll(list1);

				}
			}
		}
		result.addAll(list);
		return result;
	}

	@Override
	public TableOutput<LogSee> queryLogByuserId(TableInput input, long userId) {
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("searchs", input.getSearch());
		List<Long> list = null;
		list = queryChildAll(userId);
		String uid = String.valueOf(userId);
		if (list != null) {
			for (Long l : list) {
				uid = uid + "," + String.valueOf(l);
			}
		}
		conditions.put("uid", uid);
		if (input.getOrder() != null) {
			conditions.put("orderColumn", input.getOrder().getColumn());
			conditions.put("orderDirection", input.getOrder().getDir());
		}
		conditions.put("start", input.getStart());
		conditions.put("rows", input.getRows());
		int total = logMapper.queryLogByuserIdAll(conditions).size();
		List<LogSee> logSeelist = logMapper.queryLogByuserId(conditions);
		TableOutput<LogSee> output = new TableOutput<LogSee>();
		output.setTotal(total);
		output.setRows(input.getRows());
		output.setStart(input.getStart());
		output.setList(logSeelist);
		return output;
	}

	@Override
	public void delete(long userId) {
		logMapper.delete(userId);
	}

}
